package fm.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fm.dto.AdminUser;
import fm.dto.NeiyiUser;
import fm.dto.SystemUser;
import fm.entity.PublicNumber;
import fm.dto.NeiyiUser;
import fm.exception.BizException;
import fm.exception.TokenInvalidException;
import fm.util.CommonUtils;
import fm.util.Constant;
import fm.util.HttpRequestUtils;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.security.auth.login.LoginException;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @修改人：CM
 * @修改时间：2017/2/27 20:46
 */
@Component
public class CurrentRequest {


    public static Object getCurrentLoginUser() throws Exception {
        Object user = getSession().getAttribute(Constant.SESSION_LOGIN_USER);
        if (user == null) {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
        return user;
    }

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpSession getSession() throws TokenInvalidException {
        HttpSession session = getRequest().getSession();
        if (session == null) {
            throw new TokenInvalidException("登录超时");
        }
        return getRequest().getSession();
    }

    public static void addSessionAttribute(String key, Object val) throws TokenInvalidException {
        if (getSession() != null) {
            getSession().setAttribute(key, val);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public static void addRequestAttribute(String key, String val) throws TokenInvalidException {
        if (getRequest() != null) {
            getRequest().setAttribute(key, val);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public static Object getRequestAttribute(String key) throws TokenInvalidException {
        if (getRequest() != null) {
            return getRequest().getAttribute(key);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public static Object getSessionAttribute(String key) throws TokenInvalidException {
        if (getSession() != null) {
            return getSession().getAttribute(key);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public static boolean isAdmin() throws Exception {
        Object user = getCurrentLoginUser();
        if (user instanceof NeiyiUser) {
            return false;
        } else if (user instanceof AdminUser) {
            return true;
        } else {
            throw new BizException("错误的用户类型");
        }
    }

    public static Object getCurrentUser() throws Exception {
        return getCurrentLoginUser();
    }

    public static Object getCurrentUserId() throws Exception {
        SystemUser user = (SystemUser) getCurrentLoginUser();
        return user.getId();
    }


    public static String getRemoteIp() {
        return "112.5.203.116";
//        HttpServletRequest request = getRequest();
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("PRoxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//
//        return ip;
    }


//    public static JSONObject getRemoteAddress() throws Exception {
//
////        if (getSessionAttribute("remote_address") != null) {
////            return (JSONObject) getSessionAttribute("remote_address");
////        } else {
//        String url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + getRemoteIp();
//        String result = HttpRequestUtils.postJson(url,new JSONObject());
//        JSONObject resultObj = JSON.parseObject(result);
//        JSONObject data = resultObj.getJSONObject("data");
//        for (String key : data.keySet()) {
//            String item = StringUtils.isEmpty(data.getString(key)) ? "" : CommonUtils.decodeUnicode((String) data.get(key));
//            data.put(key, item);
//        }
//        return data;
////        }
//    }
//
//
//    public static void main(String[] args) {
//        try {
//            JSONObject address = getRemoteAddress();
//            System.out.println(JSON.toJSONString(address));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


}
